package org.zhaya.sdk.builder;

import io.logz.sawmill.Processor;
import io.logz.sawmill.utilities.JsonUtils;
import org.zhaya.sdk.model.ProcessorNode;

import java.util.Map;

/**
 * @Author Ryan Yang
 * @Date 2022/9/28 4:36 下午
 */
public class ProcessorNodeBuilder {

    /**
     * 组件节点对象
     */
    private final ProcessorNode processorNode;

    public ProcessorNodeBuilder() {
        this.processorNode = new ProcessorNode();
    }

    /**
     * 组件名称赋值
     * 不传默认使用组件类名
     */
    public ProcessorNodeBuilder name(String name) {
        this.processorNode.setName(name);
        return this;
    }

    /**
     * 配置信息赋值（配置类）
     */
    public ProcessorNodeBuilder config(Processor.Configuration config) {
        this.processorNode.setConfig(config);
        return this;
    }

    /**
     * 配置信息赋值（配置Map）
     */
    public ProcessorNodeBuilder configMap(Map<String, Object> configMap, Class<? extends Processor.Configuration> clazz) {
        this.processorNode.setConfig(JsonUtils.fromJsonMap(clazz, configMap));
        return this;
    }

    /**
     * 配置信息赋值（配置Json）
     */
    public ProcessorNodeBuilder configJson(String configJson, Class<? extends Processor.Configuration> clazz) {
        this.processorNode.setConfig(JsonUtils.fromJsonString(clazz, configJson));
        return this;
    }

    public ProcessorNode build() {
        return processorNode;
    }
}
